package com.itheima.mapper;

import com.itheima.domain.Student;
import org.apache.ibatis.annotations.Many;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.mapping.FetchType;

import java.util.List;

public interface StudentMapper {

    /*
    # 查询学生和选择课程 (一个学生选择了多门课程)
        # 1.查询所有学生
            SELECT * FROM student;
        # 2. 根据学生表id 字段的值, 去查询另外两个表
            SELECT c.* FROM stu_cr sc,course c WHERE sc.cid = c.id AND sc.sid = 1;
     */

    @Select("select * from student")
    @Results({
            //1.封装学生基本信息
            @Result(column = "id",property = "id",id = true),
            @Result(column = "name",property = "name"),
            @Result(column = "age",property = "age"),
            //2.封装课程的集合信息 private List<Course> courses;
            @Result(
                    property = "courses",
                    javaType = List.class,
                    column = "id",
                    many = @Many(select = "com.itheima.mapper.CourseMapper.findBySid",fetchType = FetchType.LAZY)
            )

    })
    public List<Student> findAll();
}
